package com.erik.lin.service;

import com.erik.lin.domain.NodeTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Erik on 2017/7/6.
 */
@Service
public class MenuService {

    @Autowired
    private JdbcTemplate jt;

    public NodeTree getSomeNode(List<String> menuIds) {
        NodeTree root = new NodeTree();
        root.setParent(null);
        root.setMenuId("root");
        root.setMenuName("root");
        root.setChildren(getSomeRootChildren(menuIds));


        return root;
    }

    public List<NodeTree> getSomeRootChildren(List<String> menuIds) {

        List<NodeTree> rs = new ArrayList<>();
        String sql = "select menu_id, parent_id, menu_name from dacp_menu";
        List<Map<String, Object>> maps = jt.queryForList(sql);
        Map<String, NodeTree> index = new HashMap<String, NodeTree>();
        for(Map map: maps) {
            NodeTree node = new NodeTree(map.get("parent_id") == null?"":map.get("parent_id").toString(), map.get("menu_id").toString(), map.get("menu_name").toString(), new ArrayList<NodeTree>());
            index.put(node.getMenuId(), node);
        }

//        for(Map map: maps) {
//            Object parentId = map.get("parent_id");
//            if(parentId != null && !"".equals(parentId.toString())) {
//                NodeTree parentNode = index.get(parentId);
//                String menuId = map.get("menu_id").toString();
//                NodeTree node = index.get(menuId);
//                List<NodeTree> children = parentNode.getChildren();
//                children.add(node);
//            }else {
//                rs.add(index.get(map.get("menu_id").toString()));
//            }
//        }

//        for(String menuId: menuIds) {
//            NodeTree node = index.get(menuId);
//            NodeTree parentNode = index.get(node.getParentId());
//            Iterator<NodeTree> iterator = parentNode.getChildren().iterator();
//            while (iterator.hasNext()) {
//                NodeTree nodeVar = iterator.next();
//                if(!nodeVar.getMenuId().equals(menuId)) {
//                    iterator.remove();
//                }
//            }
//        }
        Set<String> allMenuIds = new HashSet<>();

        allMenuIds.addAll(menuIds);

        for(String menuId: menuIds) {
            addParentIds(menuId, index, allMenuIds);
        }

        Map<String, NodeTree> index2 = new HashMap<>();

        for(String id: allMenuIds) {
            index2.put(id, index.get(id));
        }

        for(Map.Entry<String, NodeTree> entry: index2.entrySet()) {
            Object parentId = entry.getValue().getParentId();
            if(parentId != null & !"".equals(parentId)) {
                NodeTree parentNode = index2.get(parentId);
                parentNode.getChildren().add(entry.getValue());
            }else {
                rs.add(entry.getValue());
            }
        }

        return rs;
    }

    public void addParentIds(String menuId, Map<String, NodeTree> index, Set<String> allMenuIds) {
        String parentId = index.get(menuId).getParentId();
        if(parentId != null & !"".equals(parentId)) {
            allMenuIds.add(parentId);
            addParentIds(parentId, index, allMenuIds);
        }

    }

    public NodeTree getMenuNode() {
        NodeTree root = new NodeTree();
        root.setParent(null);
        root.setMenuId("root");
        root.setMenuName("root");
        root.setChildren(getRootChildren());
        return root;
    }

    public List<NodeTree> getRootChildren() {
        List<NodeTree> rs = new ArrayList<>();
        String sql = "select menu_id, parent_id, menu_name from dacp_menu";
        List<Map<String, Object>> maps = jt.queryForList(sql);
        Map<String, NodeTree> index = new HashMap<String, NodeTree>();
        for(Map map: maps) {
            NodeTree node = new NodeTree(null, map.get("menu_id").toString(), map.get("menu_name").toString(), new ArrayList<NodeTree>());
            index.put(node.getMenuId(), node);
        }

        for(Map map: maps) {
            Object parentId = map.get("parent_id");
            if(parentId != null && !"".equals(parentId.toString())) {
                NodeTree parentNode = index.get(parentId);
                String menuId = map.get("menu_id").toString();
                NodeTree node = index.get(menuId);
                List<NodeTree> children = parentNode.getChildren();
                children.add(node);
            }else {
                rs.add(index.get(map.get("menu_id").toString()));
            }
        }

        return rs;
    }
 }
